import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { type INestApplication } from '@nestjs/common';
import { createLogger } from '@/common/logger/common';
import { ConfigService } from '@nestjs/config';
import { AllConfigType } from '@/config/config.type';
const logger = createLogger();
export const initSwagger = (app: INestApplication) => {
  const configService = app.get(ConfigService<AllConfigType>);
  const appConfig = configService.getOrThrow('app', { infer: true });
  if (!appConfig.enableSwagger) return;
  const options = new DocumentBuilder()
    .setTitle('API')
    .setDescription('API docs')
    .setVersion('1.0')
    .addBearerAuth()
    .addGlobalParameters({
      in: 'header',
      required: false,
      name: process.env.APP_HEADER_LANGUAGE || 'x-custom-lang',
      schema: {
        example: 'en',
      },
    })
    .build();

  const document = SwaggerModule.createDocument(app, options);
  SwaggerModule.setup('docs', app, document);
  logger.log(`swagger ui:=> http://localhost:${appConfig.port}/docs`);
};
